package com.lyf.scm.admin.api.controller;

import com.lyf.scm.admin.api.dto.*;
import com.lyf.scm.admin.domain.service.SalesPlanDirectlyAccuracyService;
import com.lyf.scm.common.enums.ResponseMsg;
import com.lyf.scm.common.model.ExcelExportDTO;
import com.lyf.scm.common.util.ExcelExportUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

/**
 * 直营预测计划_销售计划准确率
 *
 * @author wangchlei
 * @date 2020/08/13
 */
@Slf4j
@Api(tags = "直营预测计划_销售计划准确率")
@RequestMapping("/api/v1/salesPlanDirectlyAccuracy")
@RomeController
public class SalesPlanDirectlyAccuracyController {
    @Resource
    private SalesPlanDirectlyAccuracyService salesPlanDirectlyAccuracyService;

    @PostMapping("/listWeek")
    @ApiOperation(value = "按条件查询直营预测计划_销售计划准确率(周)")
    public Response<PlanResponseDTO<SalesPlanDirectlyAccuracyInfoDTO>> listWeekBySelective(@RequestBody SalesPlanDirectlyAccuracyDTO salesPlanDirectlyAccuracyDTO) {
        try {
            return Response.builderSuccess(salesPlanDirectlyAccuracyService.listWeekBySelective(salesPlanDirectlyAccuracyDTO));
        } catch (feign.RetryableException e) {
            e.printStackTrace();
            return Response.builderFail(ResponseMsg.EXCEPTION_SC_1004.getCode(), ResponseMsg.EXCEPTION_SC_1004.getMsg());
        }
    }

    @PostMapping("/eChartsData")
    @ApiOperation(value = "图表数据接口")
    public Response<EChartsDTO> eChartsData(@RequestBody SalesPlanDirectlyAccuracyDTO salesPlanDirectlyAccuracyDTO) {
        try {
            return Response.builderSuccess(salesPlanDirectlyAccuracyService.eChartsData(salesPlanDirectlyAccuracyDTO));
        } catch (feign.RetryableException e) {
            e.printStackTrace();
            return Response.builderFail(ResponseMsg.EXCEPTION_SC_1004.getCode(), ResponseMsg.EXCEPTION_SC_1004.getMsg());
        }
    }

    @PostMapping("/export")
    @ApiOperation(value = "导出")
    public void export(@RequestBody ExcelExportDTO excelExportDTO, @RequestParam Integer type, HttpServletResponse httpServletResponse) {
        try {
            //获得feign响应流对象
            feign.Response response = salesPlanDirectlyAccuracyService.export(excelExportDTO, type);
            //如果返回普通json对象，处理异常响应
            if (ExcelExportUtil.validJsonResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                ExcelExportUtil.handleProviderErrorResponse(response, httpServletResponse);
            }
            //如果返回excel对象，处理正确响应
            if (ExcelExportUtil.validExcelResponse(response.headers().get(ExcelExportUtil.HEAD_CONTENT_TYPE))) {
                String sourceFileName = "直营预测计划_销售计划准确率";
                ExcelExportUtil.handleProviderRightResponse(response, httpServletResponse, sourceFileName);
            }
        } catch (Exception e) {
            ExcelExportUtil.handleExceptionResponse(httpServletResponse, e);
        }
    }
}
